/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.ipti.br.tag.pdf;

import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Element;
import com.itextpdf.text.Image;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import org.ipti.br.tag.entity.Human;
import org.ipti.br.tag.entity.Unity;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.List;
import javax.ejb.Stateless;
import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;

/**
 *
 * @author FelipeMSX
 */
@Stateless
public class ConsentTermPdf extends AbstractPdf {
    
    public void run(List<Human> humans, Unity unity)
            throws FileNotFoundException, DocumentException, IOException, Exception {
        
        this.init("Termo" + "-" + unity.getName());
        
        for (Human human : humans) {
            generateScope(human);
            newPage();
        }
        close();
        export();
    }
    
    public void generateScope(Human human) throws Exception {
        ExternalContext externalContext = FacesContext.getCurrentInstance().getExternalContext();
        Image image;

        image = Image.getInstance(externalContext.getRealPath("SLDI.jpg"));
        image.scaleAbsoluteWidth(130);
        image.scaleAbsoluteHeight(60);
        image.setAlignment(Element.ALIGN_CENTER);	
        Paragraph paragraphSpace = new Paragraph("",super.getFontTextBold());
        paragraphSpace.setSpacingAfter(7);
        Paragraph paragraph1 = new Paragraph("Autorização para que seu filho participe de uma campanha de saúde na escola ",super.getFontTextBold());
        paragraph1.setAlignment( Element.ALIGN_CENTER);
        paragraph1.setSpacingAfter(15);

        Paragraph paragraph2 = new Paragraph("Prezado(a) Senhor(a)", super.getFontTextNORMAL());
        paragraph2.setSpacingAfter(7);

        Paragraph paragraph3 = new Paragraph("Gostaríamos de convidar seu filho para participar de uma campanha de "
                + "saúde que será realizada em sua escola e, para isto, pedimos a sua autorização.", super.getFontTextNORMAL());
        paragraph3.setFirstLineIndent( super.getSpaceToFirstLine() );

        Paragraph paragraph4 = new Paragraph("Caso o senhor(a) concorde, o seu filho(a) será submetido a uma punção da extremidade "
                + "do dedo médio da mão esquerda, com lancetador de lancetas descartáveis, para a obtenção de uma pequena gota de"
                + " sangue. Esta punção será feita por profissional treinado e a criança sentirá somente um pequeno desconforto,"
                + " sendo que não há riscos à sua saúde. Com esta gota de sangue, faremos a dosagem da concentração de hemoglobina,"
                + " dado que será utilizado para o diagnóstico de anemia.",super.getFontTextNORMAL());
        paragraph4.setFirstLineIndent( super.getSpaceToFirstLine() );

        Paragraph paragraph5 = new Paragraph("Caso o senhor concorde, por favor preencha o nome de seu filho e assine.", super.getFontTextNORMAL());
        paragraph5.setFirstLineIndent( super.getSpaceToFirstLine() );
        paragraph5.setSpacingAfter(15);

        Paragraph paragraph6 = new Paragraph("Nome da criança ou adolescente:     "+human.getName(), getFontTextBold());
        paragraph6.setSpacingAfter(10);
        Paragraph paragraph7 = null;
        Paragraph paragraph122 = null;

        if(human.getFather() != null) {
            paragraph7 = new Paragraph("[ ] - Nome do Pai: _______________________________________________________________________", super.getFontTextNORMAL());
            paragraph7.setSpacingAfter(2);

            paragraph122 = new Paragraph("                                                         "+
                    human.getFather().getName(), super.getFontTextBold());
            paragraph122.setSpacingAfter(15);
        }

        Paragraph paragraph18 = null;
        Paragraph paragraph133 = null;
        if(human.getMother() != null){
            paragraph18 = new Paragraph("[ ] - Nome da Mãe: _______________________________________________________________________", super.getFontTextNORMAL());
            paragraph18.setSpacingAfter(2);

            paragraph133 = new Paragraph("                                                         "+
                    human.getMother().getName(),super.getFontTextBold());
            paragraph133.setSpacingAfter(15);
        }
        Paragraph paragraph9 = null;
        Paragraph paragraph999 = null;

        if(human.getSponsor() != null){
            paragraph9  = new Paragraph("[ ] - Assinatura do responsável: ______________________________________________________",super.getFontTextNORMAL());
            paragraph9.setSpacingAfter(2);
            paragraph999 = new Paragraph("                                                                         "+
                     human.getSponsor().getName(),super.getFontTextBold());
            paragraph999.setSpacingAfter(15);
        } 
        Image digital;
        digital = Image.getInstance(externalContext.getRealPath("Square.jpg"));
        digital.scaleAbsoluteWidth(80);
        digital.scaleAbsoluteHeight(100);
        digital.setAlignment(Image.RIGHT|Image.TEXTWRAP);

        Paragraph paragraph111 = new Paragraph("  ");
        paragraph111.setSpacingAfter( 10);
        PdfPTable tableWH = new PdfPTable(3);        
        tableWH.setWidthPercentage(100);
        PdfPCell cellWeight = new PdfPCell(new Paragraph("Peso",super.getFontTextBold())); 
        PdfPCell cellHeight = new PdfPCell(new Paragraph("Altura",super.getFontTextBold()));
        PdfPCell cellDate = new PdfPCell(new Paragraph("Data de Coleta",super.getFontTextBold()));
        PdfPCell cellSpacing = new PdfPCell(new Paragraph("     ",super.getFontTextBold()));
        tableWH.addCell(cellWeight);
        tableWH.addCell(cellHeight);
        tableWH.addCell(cellDate );
        tableWH.addCell( cellSpacing );
        cellSpacing = new PdfPCell(new Paragraph("     ",super.getFontTextBold()));
        tableWH.addCell( cellSpacing);
        cellSpacing = new PdfPCell(new Paragraph("     ",super.getFontTextBold()));
        tableWH.addCell( cellSpacing);
        tableWH.setSpacingAfter(10);

        PdfPTable tableHB = new PdfPTable(2);
        tableHB.setWidthPercentage(100);
        PdfPCell cellHB = new PdfPCell(new Paragraph("HB 1",super.getFontTextBold())); 
        PdfPCell cellDate2 = new PdfPCell(new Paragraph("Data Coleta",super.getFontTextBold()));
        tableHB.addCell( cellHB );
        tableHB.addCell( cellDate2 );
        cellSpacing = new PdfPCell(new Paragraph("     ",super.getFontTextBold()));
        tableHB.addCell( cellSpacing);
        cellSpacing = new PdfPCell(new Paragraph("     ",super.getFontTextBold()));
        tableHB.addCell( cellSpacing);
        cellHB = new PdfPCell(new Paragraph("HB 2",super.getFontTextBold())); 
        cellDate2 = new PdfPCell(new Paragraph("Data Coleta",super.getFontTextBold()));
        tableHB.addCell( cellHB );
        tableHB.addCell( cellDate2 );
        cellSpacing = new PdfPCell(new Paragraph("     ",super.getFontTextBold()));
        tableHB.addCell( cellSpacing);
        cellSpacing = new PdfPCell(new Paragraph("     ",super.getFontTextBold()));
        tableHB.addCell( cellSpacing);
        cellHB = new PdfPCell(new Paragraph("HB 3",super.getFontTextBold())); 
        cellDate2 = new PdfPCell(new Paragraph("Data Coleta",super.getFontTextBold()));
        tableHB.addCell( cellHB );
        tableHB.addCell( cellDate2 );
        cellSpacing = new PdfPCell(new Paragraph("     ",super.getFontTextBold()));
        tableHB.addCell( cellSpacing);
        cellSpacing = new PdfPCell(new Paragraph("     ",super.getFontTextBold()));
        tableHB.addCell( cellSpacing);
        tableHB.setSpacingAfter(8);

        Paragraph paragraph222 = new Paragraph ("[ ] - Sulfato ferroso: __________________________________________________",super.getFontTextNORMAL());
        paragraph222.setSpacingAfter(5);
        Paragraph paragraph333 = new Paragraph ("[ ] - Vermifugo: _____________________________________________________",super.getFontTextNORMAL());


        super.getDocument().add( image );
        super.getDocument().add( paragraphSpace );
        super.getDocument().add( paragraph1 );
        super.getDocument().add( paragraph2 );
        super.getDocument().add( paragraph4 );
        super.getDocument().add( paragraph5 );
        super.getDocument().add( paragraph6 );


        super.getDocument().add(digital);  
        if(human.getFather() != null) {
            super.getDocument().add( paragraph7 );
            super.getDocument().add( paragraph122 );
        }
        if(human.getMother() != null) {
            super.getDocument().add( paragraph18 );
            super.getDocument().add( paragraph133);
        }
        if(human.getSponsor() != null) {
            super.getDocument().add( paragraph9);
            super.getDocument().add( paragraph999);
        }
        super.getDocument().add( paragraph111);
        super.getDocument().add( tableWH );
        super.getDocument().add( tableHB );

        super.getDocument().add( paragraph222);
        super.getDocument().add( paragraph333);
    }
}
